/*
*六格抓狐狸游戏
* @authorgan
* @date2012.02.14
**/
package {
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class adsdad extends MovieClip {
		
		private var width_Num:int=15;
		private var height_Num:int=15;
		private var w_array:Array=[];
		
		private var huliX:int;
		private var huliY:int;
		
		private var catx:int;
		private var caty:int;
		private var catdir:int=0;
		
		private var xianjingshu:int=0;
		
		private var addx1:Array=[1,0,-1,-1,-1,0];
		private var addy0:Array=[0,1,1,0,-1,-1];
		private var addx0:Array=[1,1,0,-1,0,1];
		private var addy1:Array=[0,1,1,0,-1,-1];
		
		private var ld:Array=[];
		private var lx:Array=[];
		private var ly:Array=[];
		private var lx2:Array=[];
		private var ly2:Array=[];
		public var boolean:Boolean = false;
		private var square_mc:Class;
		
		public function adsdad() 
		{
			this['restate_btn'].addEventListener(MouseEvent.CLICK,Start);
			this['close_btn'].addEventListener(MouseEvent.CLICK,Close);
			
			square_mc = loaderInfo.applicationDomain.getDefinition('square_mc') as Class;
			reStart();
		};
		
		/**
		 *初始化菱形 
		 */		
		private function reStart():void 
		{
			
			this['xianjing_txt'].text="0";
			for (var i:int = 0; i<width_Num; i++) 
			{
				var h_array:Array=[];
				
				for (var j:int = 0; j<height_Num; j++) {
					var _mc:MovieClip=new square_mc  ;
					
					_mc.x= j%2 ? (i*(_mc.width+3)-378):(i*(_mc.width+3)-378+ (_mc.width/2+1));
					_mc.y=j*(_mc.height*2/3+4)-310;
					this['bg_mc'].addChild(_mc);
					_mc["X"]=i;
					_mc["Y"]=j;
					_mc["Num"]=-1;
					_mc["HAO"]=-1;
					_mc.alpha=0;
					_mc.name="MC_"+i+"_"+j;
					_mc._btn.mouseEnabled=false;
					h_array.push(_mc);
				}
				w_array.push(h_array);
			}
			restat();
		}
		
		/**
		 * 
		 * 初始化 菱形属性 并添加 鼠标点击事件
		 */		
		private function restat():void {
			this['xianjing_txt'].text="0";
			catx=Math.floor(width_Num/2);
			caty=Math.floor(height_Num/2);
			MoveClip(catx,caty);
			this['all_huli'].huli_mc["X"]=catx;
			this['all_huli'].huli_mc["Y"]=caty;
			this['all_huli'].huli_mc.gotoAndStop(1);
			
			for (var i:int = 1; i<width_Num-1; i++) 
			{
				for (var j:int = 1; j<height_Num-1; j++) 
				{
					var _mc:MovieClip=this['bg_mc'].getChildByName("MC_"+i+"_"+j) as MovieClip;
					_mc["Num"]=0;
					_mc["HAO"]=-1;
					_mc.gotoAndStop(1)
					if (panduan(i,j)) 
					{
						_mc.alpha=0;
					} else 
					{
						_mc._btn.mouseEnabled=true;
						_mc.buttonMode=true;
						_mc.addEventListener(MouseEvent.CLICK,_Click);
						_mc.alpha=1;
					}
				}
			}
			nandu();
		}
		
		/**
		 *设置游戏难以成度 即 捕兽夹的数量 
		 * 
		 */		
		private function nandu():void {
			
			for (var i:int = 0; i<7; i++) {
				
				var _intX:int=Math.floor(Math.random()*10+2);
				var _intY:int=Math.floor(Math.random()*10+2);
				if (_intX==catx&&_intY==caty) {
					continue;
				}
				var _mc:MovieClip=w_array[_intX][_intY] as MovieClip;
				
				_mc.gotoAndStop(2);
				_mc.removeEventListener(MouseEvent.CLICK,_Click);
				_mc.buttonMode=false;
				_mc["Num"]=1;
			}
		}
		/**
		 *点击 
		 * @param e
		 * 
		 */		
		
		private function _Click(e:MouseEvent):void {
			var _mc:MovieClip=e.currentTarget as MovieClip;
			var X:int=_mc["X"];
			var Y:int=_mc["Y"];
			if (this['all_huli'].huli_mc["X"]==X&&this['all_huli'].huli_mc["Y"]==Y) {
				return;
			}
			_mc.gotoAndStop(2);
			_mc.removeEventListener(MouseEvent.CLICK,_Click);
			_mc.buttonMode=false;
			xianjingshu++;
			this['xianjing_txt'].text=""+xianjingshu;
			_mc["Num"]=1;
			_mc["HAO"]=-1;
			trace("X:"+_mc.x+",Y:"+_mc.y);
			huliMove();
		}
		
		/**
		 *狐狸移动 
		 * 
		 */		
		private function huliMove():void {
			
			huliX=this['all_huli'].huli_mc["X"];
			huliY=this['all_huli'].huli_mc["Y"];
			MovieClip(w_array[huliX][huliY])["HAO"]=0;
			var yuan_mc:MovieClip=w_array[huliX][huliY] as MovieClip;
			
			trace(huliX,huliY);
			if (huliX==2||huliX==12||huliY==2||huliY==12) {
				LOST();
				return;
			}
			
			if (shuying(huliX,huliY)) {
				WIN();
				return;
			}
			
			var xuanlu:int;
			var lu:int;
			lx[0]=huliX;
			ly[0]=huliY;
			
			var po:int=1;
			var Iint:int=1;
			var zuiduan:int=999;
			
			//=====================每个标注一个数字，几步能到终点的数字
			
			for (po = 1; po < 200; po++) {
				lu=0;
				for (var i:int = 0; i < Iint; i++) {
					
					var _X1:int=lx[i];
					var _Y1:int=ly[i];
					
					for (var j:int = 0; j < 6; j++) {
						
						var _X2:int = _Y1%2 ? (_X1+addx1[j]) : (_X1+addx0[j]);
						var _Y2:int=_Y1+addy0[j];
						if (MovieClip(w_array[_X2][_Y2])["Num"]!=0) {
							continue;
						}// end if
						if (MovieClip(w_array[_X2][_Y2])["HAO"]>=0) {
							continue;
						}// end if
						MovieClip(w_array[_X2][_Y2])["HAO"]=po;
						lx2[lu]=_X2;
						ly2[lu]=_Y2;
						lu++;
						if (panduan(_X2,_Y2)&&po<zuiduan) {
							zuiduan=po;
						}// end if
					}// end of for
				}// end of for
				if (lu==0) {
					break;
				}
				for (var c:int = 0; c < lu; ++c) {
					lx[c]=lx2[c];
					ly[c]=ly2[c];
				}// 
				Iint=lu;
			}
			trace(zuiduan);
			if (zuiduan==999) {
				randomRun();
				return;
			}
			//=================确定有几个最近终点格
			lu=0;
			for (var d:int = 1; d < width_Num-1; ++d) {
				for (var e:int = 1; e < height_Num-1; ++e) {
					if (MovieClip(w_array[d][e])["HAO"]==zuiduan&&panduan(d,e)) {
						
						trace(MovieClip(w_array[d][e])["HAO"] + "aaaaa");
						lx[lu]=d;
						ly[lu]=e;
						lu++;
						//trace(lu)
					}// end if
				}// end of for
			}// end of for
			//=====================================选一个最短的终点
			xuanlu=Math.floor(Math.random()*lu);
			var _X3:int=lx[xuanlu];
			var _Y3:int=ly[xuanlu];
			for (var f = 0; f < 200; ++f) {
				lu=0;
				for (var g = 0; g < 6; ++g) {
					
					var _X4:int=_Y3%2 ? (_X3+addx1[g]) : (_X3+addx0[g]);
					var _Y4:int=_Y3+addy0[g];
					
					if (MovieClip(w_array[_X4][_Y4])["Num"]!=0) {
						continue;
					}// end if
					
					if (MovieClip(w_array[_X4][_Y4])["HAO"]>=MovieClip(w_array[_X3][_Y3])["HAO"]||MovieClip(w_array[_X4][_Y4])["HAO"]==0) {
						continue;
					}// end if
					lx[lu]=_X4;
					ly[lu]=_Y4;
					ld[lu]=g;
					lu++;
				}// end of for
				if (lu==0) {
					break;
				}
				xuanlu=Math.floor(Math.random()*lu);
				_X3=lx[xuanlu];
				_Y3=ly[xuanlu];
				//trace("cccccccccc   "+MovieClip(w_array[_X3][_Y3])["HAO"])
				if (MovieClip(w_array[_X3][_Y3])["HAO"]==1) {
					//trace("cccccccccc   "+MovieClip(w_array[_X3][_Y3])["HAO"])
					catx=_X3;
					caty=_Y3;
					catdir=0;
					trace(catx +"    " + caty);
					for (var t:int = 0; t < 6; ++t) {
						var _X5:int=_Y3%2 ? (_X3+addx1[t]) : (_X3+addx0[t]);
						var _Y5:int=_Y3+addy0[t];
						if (MovieClip(w_array[_X5][_Y5])["HAO"]==0) {
							catdir=t+1;
							
						}// end if
					}// end of for
				}// end if
			}
			//trace(catdir+"AAAAA")
			this['all_huli'].huli_mc.gotoAndStop(catdir+1);
			yuan_mc.addEventListener(MouseEvent.CLICK,_Click);
		}
		
		public function moveStep():void {
			this['all_huli'].huli_mc.fangxiang_mc.gotoAndStop(catdir+1);
			MoveClip(catx,caty);
			
		}
		
		
		
		
		private function randomRun():void {
			
			trace("random");
			var lux:Array=[];
			var luy:Array=[];
			var _X1:int=this['all_huli'].huli_mc["X"];
			var _Y1:int=this['all_huli'].huli_mc["Y"];
			var yuan_mc:MovieClip=w_array[_X1][_Y1] as MovieClip;
			var moren:int=0;
			for (var j:int = 0; j < 6; j++) {
				var _X2:int=_Y1%2 ? (_X1+addx1[j]) : (_X1+addx0[j]);
				var _Y2:int=_Y1+addy0[j];
				
				//trace(MovieClip(w_array[_X2][_Y2])["Num"])
				if (MovieClip(w_array[_X2][_Y2])["Num"]!=0) {
					
					continue;
				}// end if
				
				if (MovieClip(w_array[_X2][_Y2])["HAO"]==1) {
					//trace("cccccccccc   "+MovieClip(w_array[_X3][_Y3])["HAO"])
					catx=_X2;
					caty=_Y2;
					catdir=0;
					
					for (var t:int = 0; t < 6; ++t) {
						var _X5:int=_Y2%2 ? (_X2+addx1[t]) : (_X2+addx0[t]);
						var _Y5:int=_Y2+addy0[t];
						
						if (MovieClip(w_array[_X5][_Y5])["HAO"]==0) {
							
							catdir=t+1;
							
						}// end if
					}// end of for
				}
				this['all_huli'].huli_mc.gotoAndStop(catdir+1);
				yuan_mc.addEventListener(MouseEvent.CLICK,_Click);
				return;
			}
			/*var c:int=Math.floor(Math.random()*moren);
			MoveClip(lux[c],luy[c]);*/
		}
		
		private function congzhi():void {
			for (var d:int = 1; d < width_Num-1; ++d) {
				for (var e:int = 1; e < height_Num-1; ++e) {
					MovieClip(w_array[d][e])["HAO"]=-1;
				}
			}
		}
		
		public function MoveClip(X:int,Y:int):void {
			
			var _mc:MovieClip=w_array[X][Y] as MovieClip;
			this['all_huli'].huli_mc.x=_mc.x;
			this['all_huli'].huli_mc.y=_mc.y;
			
			this['all_huli'].huli_mc["X"]=X;
			this['all_huli'].huli_mc["Y"]=Y;
			congzhi();
			_mc.removeEventListener(MouseEvent.CLICK,_Click);
		}
		
		
		private function LOST():void {
			removeAllListener();
			this['jieguo_mc'].gotoAndStop(2);
			//			gs.TweenLite.to(this['jieguo_mc'],0.75,{delay:0.5,y:0});
			boolean=true;
			if (this['all_huli'].huli_mc["X"]==2) 
			{
				catdir = 1
					;
			} else if (this['all_huli'].huli_mc["X"] == 12) 
			{
				catdir=4;
			}
			if (this['all_huli'].huli_mc["Y"]==2) 
			{
				catdir=2;
			} else if (this['all_huli'].huli_mc["Y"] == 12) 
			{
				catdir=5;
			}
			this['all_huli'].huli_mc.gotoAndStop(catdir+1);
			trace("lost");
		}
		
		private function WIN():void 
		{
			removeAllListener();
			//			thisjieguo_mc.gotoAndStop(1);
			//			gs.TweenLite.to(jieguo_mc,0.75,{y:0});
			trace("win");
			dispatchEvent(new Event("WIN"));
		}
		
		private function removeAllListener():void 
		{
			for (var i:int = 2; i<width_Num-2; i++)
			{
				for (var j:int = 2; j<height_Num-2; j++) 
				{
					var _mc:MovieClip=this['bg_mc'].getChildByName("MC_"+i+"_"+j) as MovieClip;
					_mc.removeEventListener(MouseEvent.CLICK,_Click);
					_mc.buttonMode=false;
				}
			}
		}
		
		private function shuying(X:int,Y:int):Boolean 
		{
			var d:int=0;
			for (var j:int = 0; j < 6; j++) 
			{
				var _X:int = Y%2 ? (X+addx1[j]) : (X+addx0[j]);
				var _Y:int=Y+addy0[j];
				if (MovieClip(w_array[_X][_Y])["Num"]==0) 
				{
					d++;
				}
			}
			if (d==0) 
			{
				return true;
			} 
			else
			{
				return false;
			}
		}
		
		private function panduan(X:int,Y:int):Boolean 
		{
			if (X==1||Y==1||X==13||Y==13) 
			{
				return true;
			}
			else 
			{
				return false;
			}
		}
		
		private function Start(e:MouseEvent):void 
		{
			//			gs.TweenLite.to(jieguo_mc,0.75,{y:-320});
			catdir = 0;
			xianjingshu = 0;
			ld = [];
			lx = [];
			ly = [];
			lx2 = [];
			ly2 = [];
			boolean = false;
			restat();
		}
		
		private function Close(e:MouseEvent):void {
			MovieClip(this.parent).removeChild(MovieClip(this));
			dispatchEvent(new Event("CLOSE"));
		}
		
		
		
	}
}